package com.ts.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.ts.common.service.ExportExcelSevice;
import com.ts.common.utils.*;
import com.ts.system.domain.SystemParamsDO;
import com.ts.system.service.SystemParamsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 系统参数
 *
 * @author bobby
 * @email bobby@126.com
 * @date 2021-12-18 12:46:48
 */

@Controller
@RequestMapping("/system/systemParams")
public class SystemParamsController {
    @Autowired
    private SystemParamsService systemParamsService;

    @GetMapping()
    @RequiresPermissions("system:systemParams:systemParams")
    String SystemParams() {
        return "system/systemParams/systemParams";
    }

    @ResponseBody
    @RequestMapping("/list")
    @RequiresPermissions("system:systemParams:systemParams")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<SystemParamsDO> systemParamsList = systemParamsService.list(query);
        int total = systemParamsService.count(query);
        PageUtils pageUtils = new PageUtils(systemParamsList, total);

        //导出 start
        if (StringUtils.isNotEmpty(UtilExportExcelPlus.getFileId(params))) {
            UtilExportExcelPlus.exportExcels(params, systemParamsList, new ExportExcelSevice() {
                @Override
                public Object changeVal(String key, Object val, JSONObject index) {
                    return val;
                }
            });
            return null;
        }
        //end 导出结束

        return pageUtils;
    }

    @GetMapping("/add")
    @RequiresPermissions("system:systemParams:add")
    String add() {
        return "system/systemParams/add";
    }

    @GetMapping("/edit/{id}")
    @RequiresPermissions("system:systemParams:edit")
    String edit(@PathVariable("id") Integer id, Model model) {
        SystemParamsDO systemParams = systemParamsService.get(id);
        model.addAttribute("systemParams", systemParams);
        return "system/systemParams/edit";
    }

    /**
     * 保存
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("system:systemParams:add")
    public Result save(SystemParamsDO systemParams) {
        Result result = Result.ok();
        systemParamsService.save(result, systemParams);
        return result;
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("system:systemParams:edit")
    public Result update(SystemParamsDO systemParams) {
        Result result = Result.ok();
        systemParamsService.update(result, systemParams);
        return result;
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("system:systemParams:remove")
    public Result remove(Integer id) {
        Result result = Result.ok();
        systemParamsService.remove(result, id);
        return result;
    }

    /**
     * 删除
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("system:systemParams:batchRemove")
    public Result remove(@RequestParam("ids[]") Integer[] ids) {
        Result result = Result.ok();
        systemParamsService.batchRemove(result, ids);
        return result;
    }

}
